package com.moli.iov.handle.alarm.biz.real.extend.platform;

import com.moli.iov.base.enums.AlarmAuthEnum;
import com.moli.iov.base.model.dto.SysParamSettingsCacheBean;
import com.moli.iov.base.remote.ISysParamSettingsRemoteService;
import com.moli.iov.handle.alarm.constants.Constant;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmSourceEnum;
import com.moli.iov.handle.alarm.enums.AlarmTypeEnum;
import com.moli.iov.handle.alarm.enums.AntiShakeEnum;
import com.moli.iov.handle.alarm.model.dto.AlarmDto;
import com.moli.iov.handle.alarm.model.dto.AuthDto;
import com.moli.iov.handle.alarm.model.dto.DisarmAlarmDto;
import com.moli.iov.handle.alarm.service.IAlarmInfoRequestService;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import com.moli.iov.protocol.model.realtime.VehicleAllReq;
import com.moli.iov.rest.RestResponse;
import java.util.Arrays;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  SOC电压低报警  低于20%
 * </p>
 *
 * @author zql
 * @since 2019/10/11 15:35
 */
@Slf4j
@Component
public class SocReal {

  @Autowired
  private IAlarmInfoRequestService alarmInfoRequestService;

  @Autowired
  private ISysParamSettingsRemoteService sysParamSettingsRemoteService;

  /**
   * SOC电压低报警  低于20%
   * @param vin
   * @param realTimeInfoReq
   */
  public void handle(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
    try{
      soc(vin, realTimeInfoReq, authDto);
    }catch (Exception e){
      log.error("SOC电压低报警 异常", e);
    }
  }

  /**
   * SOC电压低报警  低于20%
   * @param vin
   * @param realTimeInfoReq
   */
  public void soc(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
    try{
      VehicleAllReq vehicleAllReq = realTimeInfoReq.vehicleAllReq;
      if(vehicleAllReq == null){
        log.error("OC电压低报警  低于20% 失败 vehicleAllReq为null vin:{}", vin);
        return;
      }

      int soc = vehicleAllReq.getSoc();

      if(soc <= Constant.ERROR_VOLTAGE){
        return;
      }

      if(soc < getSocThreshold(vin)){

        if(authDto.getAlarmAuths().contains(AlarmAuthEnum.REAL.getFlag())){
          log.info("SOC电压低报警  低于20% 不监控报警 vin：{}", vin);
          return;
        }

        double longitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.longitude : 0;
        double latitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.latitude : 0;

        alarmInfoRequestService.alarm(new AlarmDto(
            vin,
            AlarmCodeEnum.SOC,
            AlarmTypeEnum.ALARM.getFlag(),
            AlarmSourceEnum.PLATFORM.getFlag(),
            "",
            AntiShakeEnum.YES.getFlag(),
            longitude,
            latitude));

        return;
      }

      alarmInfoRequestService.disarmAlarm(new DisarmAlarmDto(vin,
          AlarmCodeEnum.SOC.getFlag(),
          AntiShakeEnum.YES.getFlag()));
    }catch(Exception e){
      log.error("SOC电压低报警  低于20% 添加线程池异常", e);
    }

  }

  /**
   * 获取soc阈值
   * @param vin
   * @return
   */
  private long getSocThreshold(String vin) {

    try{
      RestResponse<Map<String, SysParamSettingsCacheBean>> restResponse =
          sysParamSettingsRemoteService.getSysParamSettingsCacheBeanByVin(Arrays.asList(vin));

      if(restResponse == null || !restResponse.flag || restResponse.getData() == null){
        return Constant.ALARM_SOC;
      }

      Map<String, SysParamSettingsCacheBean> map = restResponse.getData();

      SysParamSettingsCacheBean sysParamSettingsCacheBean = map.get(vin);

      if(sysParamSettingsCacheBean == null || sysParamSettingsCacheBean.getSoc() < 0){
        return Constant.ALARM_SOC;
      }

      return sysParamSettingsCacheBean.getSoc().longValue();
    }catch(Exception e){
      log.error("获取soc阈值 异常 Base服务调用 失败", e.getMessage());
      return Constant.ALARM_SOC;
    }
  }
}
